$().ready(function() {
	$("form").submit(function() {
		var $this = $(this);
		var exattr = exattrs[$this];
		if($this.attr("type")=="ajax"){//ajax方式提交表单
			var option = {};
			var handleType = $this.attr("handleType");
			if(handleType!=undefined){
				option.handleType = handleType;
			}
			$.extend(option, exattr);
			submitForm($this,option);
			return false;
		}
	});
	
	/**获取json格式数据*/
	$.fn.serializeObject = function()    
	{    
	   var o = {};    
	   var a = this.serializeArray();    
	   $.each(a, function() {    
	       if (o[this.name]) {    
	           if (!o[this.name].push) {    
	               o[this.name] = [o[this.name]];    
	           }    
	           o[this.name].push(this.value || '');    
	       } else {    
	           o[this.name] = this.value || '';    
	       }    
	   });    
	   return o;    
	};  
	
	/**表单绑定参数*/
	var exattrs = {};
	$.fn.bindAttribute = function(data){
		var $this = $(this);
		exattrs[$this] = data;
	}

})

function ajaxSend(url,type,data,option,e){
	var $subBtn = $(e);
	if($subBtn.hasClass("_disabled"))return false;
	$subBtn.prop("disabled", true).addClass("_disabled");
	try{ 
		$.ajax({
	        url: url,
	        type: type,
	        data: data,
	        dataType: "json",
	        async:(option.async!=null && option.async==false)?false:true,
	        cache: false,
	        success: function(message) {
	         	if (message.type == "success") {
	                if(option!=null){
	                	var handleType = option.handleType;
						 if(option.callback != null && typeof option.callback == "function"){
							option.callback(message);
						}
						if(handleType==-1){/**关闭当前窗口，提交父窗口的列表数据*/
							var mmgird = parent.$mmGrid;
	                		if(mmgird!=undefined){
	                			mmgird.load();
	                		}
	                		setTimeout(cancle_iframe,100);
	                	}
	            	}
	            } else {
	            	
		            	if(option!=null && option.failCallback != null && typeof option.failCallback == "function"){
							option.failCallback(message);
						} else {
							 error_win(message);
						}
	            }
	            $subBtn.prop("disabled", false).removeClass("_disabled");
	        },
	        complete: function() {
	        	//$subBtn.prop("disabled", true);
			},
			error: function (jqXHR, textStatus, errorThrown) {
			   var loginStatus = jqXHR.getResponseHeader("loginStatus");
			   if (loginStatus == "accessDenied") {
			   	   location.reload(true);
			   }else{
			   	   if(jqXHR.status!=0){
			   	   		$.message_alert(jqXHR.status+" : "+jqXHR.statusText);
			   	   }
			   	   $subBtn.prop("disabled", false);
		           if(option.error != null && typeof option.error == "function"){
					   option.error(jqXHR, textStatus, errorThrown);
				   }
			   }
			} 
	    });
    }catch(e){
    	$subBtn.prop("disabled", false).removeClass("_disabled");
        throw e;
    }
}

/**ajax请求方法*/
function ajaxSubmit(e,option){
	if(typeof option == "undefined")return false;
	var url = option.url;
	var type = option.method;
	var data = option.data;
	
	if(option!=null && option.isConfirm==true){
		var confirmText = '您确定要保存吗？';
		if(option.confirmText!=undefined)confirmText=option.confirmText;
		$.message_confirm(confirmText,function(){
			ajaxSend(url,type,data,option,e);
		})
	}else{
		ajaxSend(url,type,data,option,e);
	}
	
}

//提交表单执行
var submitForm = function() {
	var $form = arguments[0];
	/**表单的easyui验证*/
	if($form.attr("validate-type")=="validate" && !$form.valid()){
		return false;
	}
	/**禁用提交按钮*/
	var $subBtn = $form.find(".sureButton");
	if($subBtn==undefined)return false;
	var option =null;
	if(arguments.length>1){
		option = arguments[1];
	}
	
	var url = $form.attr("action");
	var type = $form.attr("method") ;
	//var data = $form.serialize();
	
	var data;
	if (option != null && option.dataType == "json") {
		data = $form.serialize();
		var extraParam;
		if(option.extraParam != null && typeof option.extraParam == "function"){
			extraParam = option.extraParam();
		}else{
			extraParam = option.extraParam;
		}
		
		data = data+"&"+jQuery.param( extraParam, true);
	} else {
		data = $form.serialize();
	}
	
	if(option!=null && option.isConfirm==true){
		var confirmText = '您确定要保存吗？';
		if(option.confirmText!=undefined)confirmText=option.confirmText;
		$.message_confirm(confirmText,function(){
			ajaxSend(url,type,data,option,$subBtn);
		})
	}else{
		ajaxSend(url,type,data,option,$subBtn);
	}
	
}
